package com.lexue.articlesync.controller;

import com.lexue.articlesync.service.CacheService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 缓存管理API控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/cache")
@RequiredArgsConstructor
public class CacheController {
    
    private final CacheService cacheService;
    
    /**
     * 获取缓存统计信息
     */
    @GetMapping("/stats")
    public ResponseEntity<CacheService.CacheStats> getCacheStats() {
        try {
            CacheService.CacheStats stats = cacheService.getCacheStats();
            log.debug("获取缓存统计信息: {}", stats);
            return ResponseEntity.ok(stats);
        } catch (Exception e) {
            log.error("获取缓存统计信息失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 手动清理过期缓存
     */
    @PostMapping("/cleanup")
    public ResponseEntity<String> cleanupExpiredCache() {
        try {
            // 获取清理前的统计信息
            CacheService.CacheStats beforeStats = cacheService.getCacheStats();
            
            // 执行清理
            cacheService.cleanExpiredFileCache();
            
            // 获取清理后的统计信息
            CacheService.CacheStats afterStats = cacheService.getCacheStats();
            
            int cleanedFiles = beforeStats.getTotalFiles() - afterStats.getTotalFiles();
            String message = String.format("缓存清理完成，删除了 %d 个过期文件", cleanedFiles);
            
            log.info(message);
            return ResponseEntity.ok(message);
            
        } catch (Exception e) {
            log.error("手动清理缓存失败", e);
            return ResponseEntity.internalServerError()
                    .body("缓存清理失败: " + e.getMessage());
        }
    }
    
    /**
     * 清理指定平台的认证缓存
     */
    @DeleteMapping("/auth/{platformCode}")
    public ResponseEntity<String> clearPlatformAuthCache(@PathVariable String platformCode) {
        try {
            int clearedFiles = cacheService.clearPlatformAuthCache(platformCode);
            String message = String.format("平台 %s 的认证缓存清理完成，删除了 %d 个文件", platformCode, clearedFiles);
            
            log.info(message);
            return ResponseEntity.ok(message);
            
        } catch (Exception e) {
            log.error("清理平台认证缓存失败: {}", platformCode, e);
            return ResponseEntity.internalServerError()
                    .body("清理失败: " + e.getMessage());
        }
    }
    
    /**
     * 清理所有缓存
     */
    @DeleteMapping("/all")
    public ResponseEntity<String> clearAllCache() {
        try {
            int clearedFiles = cacheService.clearAllCache();
            String message = String.format("所有缓存清理完成，删除了 %d 个文件", clearedFiles);
            
            log.info(message);
            return ResponseEntity.ok(message);
            
        } catch (Exception e) {
            log.error("清理所有缓存失败", e);
            return ResponseEntity.internalServerError()
                    .body("清理失败: " + e.getMessage());
        }
    }
}